package cz.cacek.ebook;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

/**
 * Holds translations for EBookME - list of resources is generated in
 * ResourceProvider class in packager application.
 *
 * @author Josef Cacek [josef.cacek (at) atlas.cz]
 * @author $Author: kwart $
 * @version $Revision: 1.4 $
 * @created $Date: 2006/03/20 15:21:22 $
 *
 */
public final class ResourceProviderME {

	/**
	 * singleton instance of ResourceProvider
	 */
	public static final ResourceProviderME provider = new ResourceProviderME();

	private Hashtable messages;

	private ResourceProviderME() {
		super();
		final Hashtable tmpMsgs = new Hashtable();
		try {
			InputStream in = getClass().getResourceAsStream(
					"/" + Utils.DATA_FOLDER + "/" + Utils.LANGUAGE_FILE);
			if (null != in) {
				UTF8ISReader tmpReader = new UTF8ISReader(in);
				char tmpChr[] = new char[1];
				StringBuffer tmpSB[] = new StringBuffer[2];
				tmpSB[0] = new StringBuffer();
				tmpSB[1] = new StringBuffer();
				int tmpPos = 0;
				boolean tmpBSlash = false;
				while (1 == tmpReader.read(tmpChr)) {
					switch (tmpChr[0]) {
					case '\n':
						tmpPos = 0;
						tmpMsgs.put(tmpSB[0].toString(), tmpSB[1].toString());
						tmpSB[0].setLength(0);
						tmpSB[1].setLength(0);
						tmpBSlash = false;
						break;
					case '=':
						tmpPos = 1;
						break;
					case '\\':
						if (tmpBSlash) {
							tmpSB[tmpPos].append('\\');
						} else {
							tmpBSlash = true;
						}
						break;
					case 'n':
						if (tmpBSlash) {
							tmpBSlash = false;
							tmpSB[tmpPos].append('\n');
							break;
						}
					default:
						if (tmpBSlash) {
							tmpSB[tmpPos].append('\\');
							tmpBSlash = false;
						}
						tmpSB[tmpPos].append(tmpChr[0]);
						break;
					}
				}
				tmpReader.close();
			}
				setMessages(tmpMsgs);

		} catch (IOException ioe) {
			throw new RuntimeException(ioe.getMessage());
		}
	}

	/**
	 * Returns message identified by given key.
	 *
	 * @param aKey
	 * @return message identified by given key
	 */
	public synchronized static String get(final String aKey) {
		final String tmpResult = (String) provider.getMessages().get(aKey);
		if (tmpResult == null) {
			return aKey;
		}
		return tmpResult;
	}

	public synchronized Hashtable getMessages() {
		return messages;
	}

	private synchronized void setMessages(final Hashtable aMsgs) {
		messages = aMsgs;
	}
}
